/**
 * 外部插入 JS
 */
; (function () {
  /** docoment 加载完成 */
  function domReady() {
    const condition = arguments.length ? Array.from(arguments) : ['complete', 'interactive'];
    return new Promise(resolve => {
      if (condition.includes(document.readyState)) {
        resolve(true);
      } else {
        document.addEventListener('readystatechange', () => {
          if (condition.includes(document.readyState)) {
            resolve(true);
          }
        });
      }
    });
  }

  /** element 选择器 */
  function el(selector = '') {
    const els = document.querySelectorAll(selector);
    if (!els.length) {
      return null;
    } else if (els.length === 1) {
      return els[0];
    } else {
      return els;
    }
  }

  /** 创建 DOM */
  function creDom(arg0) {
    arg0 = arg0 || {};
    const dom = document.createElement(arg0.tag || 'div');

    if (typeof arg0.style === 'object') {
      creDom.prototype.setDomStyle(dom, arg0.style);
    } else if (typeof arg0.cssText === 'string') {
      dom.style.cssText += arg0.cssText;
    }
    if (typeof arg0.className !== 'undefined') {
      creDom.prototype.setDomClassName(dom, arg0.className);
    }

    dom.setStyle = creDom.prototype.setDomStyle;
    dom.setClassName = creDom.prototype.setDomClassName;

    return dom;
  }
  creDom.prototype.setDomStyle = (dom, style) => {
    for (const [k, v] of Object.entries(style)) dom.style[k] = v;
  };
  creDom.prototype.setDomClassName = (dom, className) => {
    if (typeof className === 'string') {
      dom.classList.add(className);
    } else if (Array.isArray(className)) {
      dom.classList.add.apply(null, className);
    }
  };

  // 插入 Button
  const oButton = creDom({ tag: 'button' });
  oButton.innerHTML = '==== 自定义按钮 ====';
  el('body').appendChild(oButton);

}());